//
//  DeviceTypeListVC.swift
//  kxf
//
//  Created by LNS2 on 2024/4/21.
//

import Foundation


class DeviceTypeListVC : WHBaseViewVC {
    
    var dataSourceArray = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        initUI()
        sendDeviceTypeListRequest()
    }
    lazy var topWhiteView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(10), width: SCREEN_WIDHT, height: kFitWidth(48)))
        vi.backgroundColor = .white
        vi.layer.cornerRadius = kFitWidth(8)
        vi.clipsToBounds = true
        
        return vi
    }()
    lazy var leftLineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .THEME
        vi.layer.cornerRadius = kFitWidth(2)
        
        return vi
    }()
    lazy var titleLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK333
        lab.text = "总设备数"
        lab.font = .systemFont(ofSize: 16, weight: .semibold)
        
        return lab
    }()
    lazy var numberLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.text = "0"
        lab.font = .systemFont(ofSize: 16, weight: .semibold)
        return lab
    }()
    lazy var numberLab: UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK333
        lab.text = "个"
        lab.font = .systemFont(ofSize: 16, weight: .semibold)
        return lab
    }()
    lazy var tableView : UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: self.topWhiteView.frame.maxY, width: SCREEN_WIDHT, height: SCREEN_HEIGHT - self.topWhiteView.frame.maxY), style: .plain)
        vi.register(DeviceTypeTableViewCell.classForCoder(), forCellReuseIdentifier: "DeviceTypeTableViewCell")
        vi.delegate = self
        vi.dataSource = self
        vi.backgroundColor = .clear
        vi.separatorStyle = .none
        return vi
    }()
}

extension DeviceTypeListVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSourceArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceTypeTableViewCell")as! DeviceTypeTableViewCell
        
        let dict = self.dataSourceArray[indexPath.row]as? NSDictionary ?? [:]
        cell.updateUI(dict: dict)
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kFitWidth(102)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dict = self.dataSourceArray[indexPath.row]as? NSDictionary ?? [:]
        if dict["type"]as? String ?? "" == "CODE_PLATE" {
            let vc = DeviceSNListVC()
            vc.deviceType = "CODE_PLATE"
            self.navigationController?.pushViewController(vc, animated: true)
        }else if dict["type"]as? String ?? "" == "VOICE_BOX" {
            let vc = DeviceVoiceListVC()
            vc.deviceType = "VOICE_BOX"
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
}
extension DeviceTypeListVC{
    func initUI() {
        initNavi(titleStr: "设备列表")
        view.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        view.addSubview(topWhiteView)
        topWhiteView.addSubview(leftLineView)
        topWhiteView.addSubview(titleLab)
        topWhiteView.addSubview(numberLab)
        topWhiteView.addSubview(numberLabel)
        
        view.addSubview(tableView)
        setConstrait()
    }
    func setConstrait() {
        leftLineView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(13))
            make.centerY.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(4))
            make.height.equalTo(kFitWidth(16))
        }
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(26))
            make.centerY.lessThanOrEqualToSuperview()
        }
        numberLab.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-10))
            make.centerY.lessThanOrEqualToSuperview()
        }
        numberLabel.snp.makeConstraints { make in
            make.right.equalTo(numberLab.snp.left)
            make.centerY.lessThanOrEqualToSuperview()
        }
    }
}

extension DeviceTypeListVC{
    func sendDeviceTypeListRequest() {
        WHNetworkUtil.shareManager().POST(urlString: URL_Device_type_list, parameters: nil,isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "\(responseObject)")
            
            let dataObj = responseObject["data"]as? NSDictionary ?? [:]
            self.dataSourceArray = dataObj["list"]as? NSArray ?? []
            self.numberLabel.text = dataObj["totalNumber"]as? String ?? "00"
            
            
            self.tableView.reloadData()
        }
    }
}
